<?php

namespace app\admin\model;

use think\Model;


class ClothmakingPlan extends Model
{

    

    

    // 表名
    protected $name = 'clothmaking_plan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'formula_process_id',
        'coating_process_id',
        'plc_tech_id',
        'product_inspection_id',
        'packing_inspection_id',
        'product_id'
    ];

    public function formulaProcess()
    {
        return $this->belongsTo('ProductionRecipeProcess', 'formula_process_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function coatingProcess()
    {
        return $this->belongsTo('ProductionCoatingProcess', 'coating_process_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function plcTech()
    {
        return $this->belongsTo('ClothmakingBoilerPlc', 'plc_tech_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function productInspection()
    {
        return $this->belongsTo('Product', 'product_inspection_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function packingInspection()
    {
        return $this->belongsTo('PackageMaterial', 'packing_inspection_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function product()
    {
        return $this->belongsTo('Product', 'product_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function recipe()
    {
        return $this->belongsTo('ClothmakingRecipe', 'recipe_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function order()
    {
        return $this->hasOne('ClothmakingOrders', 'id', 'order_id');
    }
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'notstarted' => __('Notstarted'), 'completed' => __('Completed')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
